package com.hp.gxw.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.gxw.constant.LayTableR;
import com.hp.gxw.entity.SaleOrder;
import com.hp.gxw.service.ICustomerService;
import com.hp.gxw.service.ISaleOrderService;
import com.hp.gxw.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/sale_report")
public class SaleReportController {

    @Autowired
    private ISaleOrderService iSaleOrderService;

    @Autowired
    private ICustomerService iCustomerService;

    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("customer", iCustomerService.list());
        return "report/sale_report/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableR list(Integer page, Integer limit, String master) throws Exception {
        QueryWrapper<SaleOrder> ew = ConditionUtil.convert(master, new QueryWrapper<>());
        Page<Map<String,Object>> pageList = iSaleOrderService.getSaleReportPage(new Page<>(page, limit), ew);
        return LayTableR.result(0, pageList.getTotal(), pageList.getRecords());
    }

    @RequestMapping("/download")
    @ResponseBody
    public void download(String condition, HttpServletResponse response) {
        List<Map> conditionList = JSON.parseArray(condition, Map.class);
        try {
            String queryCriteriaStr = "【查询条件】";
            QueryWrapper qw = new QueryWrapper();
            if(conditionList.size() > 0){
                for(Map m : conditionList){
                    queryCriteriaStr = queryCriteriaStr+m.get("title")+": "+m.get("value")+";   ";
                    switch (m.get("type").toString()){
                        case "equal":
                            qw.eq(m.get("field").toString(), m.get("value"));
                            break;
                        case "like":
                            qw.like(m.get("field").toString(), m.get("value"));
                            break;
                        case "unequal":
                            qw.ne(m.get("field").toString(), m.get("value"));
                            break;
                        case "empty":
                            qw.isNull(m.get("field").toString());
                            break;
                        case "notempty":
                            qw.isNotNull(m.get("field").toString());
                            break;
                        case "between":
                            qw.between(m.get("field").toString(), m.get("left"), m.get("right"));
                            break;
                        case "greater_than":
                            qw.gt(m.get("field").toString(), m.get("value"));
                            break;
                        case "less_than":
                            qw.lt(m.get("field").toString(), m.get("value"));
                            break;
                        case "start":
                            qw.likeLeft(m.get("field").toString(), m.get("value"));
                            break;
                        case "end":
                            qw.likeRight(m.get("field").toString(), m.get("value"));
                            break;
                    }
                }
            }else{
                queryCriteriaStr = queryCriteriaStr+"无查询条件，查询全表";
            }
            //excel表头
            List<TitleEntity> titleList=new ArrayList<>();
            titleList.add(new TitleEntity("0",null,"销售订单报表",null));
            titleList.add(new TitleEntity("1","0",queryCriteriaStr,null));
            titleList.add(new TitleEntity("2","1","销售订单","order_no"));
            titleList.add(new TitleEntity("3","1","客户编码","customer_code"));
            titleList.add(new TitleEntity("4","1","客户名称","customer_name"));
            titleList.add(new TitleEntity("5","1","客户订单号","customer_order"));
            titleList.add(new TitleEntity("6","1","关联订单号","related_order"));
            titleList.add(new TitleEntity("7","1","业务员","person_name"));
            titleList.add(new TitleEntity("8","1","订单创建时间","create_time"));
            titleList.add(new TitleEntity("9","1","产品编码","cinvcode"));
            titleList.add(new TitleEntity("10","1","产品名称","cinvname"));
            titleList.add(new TitleEntity("11","1","类型","type"));
            titleList.add(new TitleEntity("12","1","批号","cbatch"));
            titleList.add(new TitleEntity("13","1","订单数量","qty"));
            titleList.add(new TitleEntity("14","1","订单金额","amount"));
            titleList.add(new TitleEntity("15","1","累计结算数量","settlement_qty"));
            titleList.add(new TitleEntity("16","1","累计结算金额","settlement_amount"));
            titleList.add(new TitleEntity("17","1","工序序列号","serial_number"));
            titleList.add(new TitleEntity("18","1","订单工序编码","order_process"));
            titleList.add(new TitleEntity("19","1","订单工序名称","order_process_name"));
            titleList.add(new TitleEntity("20","1","计价方式","pricing_method"));
            titleList.add(new TitleEntity("21","1","标准工时","expect_hours"));
            titleList.add(new TitleEntity("22","1","工序单价","ioritaxcost"));
            titleList.add(new TitleEntity("23","1","订单工序金额","ioritaxamount"));
            //excel数据
            List<Map<String,Object>> list = iSaleOrderService.getSaleReportList(qw);
            for (Map<String, Object> map : list) {
                if(map.get("pricing_method").equals("计时")){
                    map.put("ioritaxamount","标准工时*工序单价*订单数量="+new BigDecimal(map.get("expect_hours").toString()).multiply(new BigDecimal(map.get("ioritaxcost").toString())).multiply(new BigDecimal(map.get("qty").toString())));
                }else{
                    map.put("ioritaxamount","工序单价*订单数量="+new BigDecimal(map.get("ioritaxcost").toString()).multiply(new BigDecimal(map.get("qty").toString())));
                }
            }
            ExcelTool excelTool = new ExcelTool("销售订单报表",20,20);
            List<Column>  titleData = excelTool.columnTransformer(titleList,"t_id","t_pid","t_content","t_fielName","0");
            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("销售订单报表"+ DateUtil.currentYYMMDD()+".xlsx", "utf-8"));
            excelTool.exportExcel(titleData,list, response.getOutputStream(),true,true);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
